package cn.lil.servlet;

import cn.lil.dao.BearingDao;
import cn.lil.model.Bearing;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ShoppingServlet", value = "/ShoppingServlet")
public class ShoppingServlet extends HttpServlet {
    BearingDao bearingDao = new BearingDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Bearing> bearings = new ArrayList<Bearing>();
        try {
            System.out.println(bearingDao.getAllBearing());
            bearings = bearingDao.getAllBearing();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("bearings", bearings);
        System.out.println(bearings);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/shopping.jsp");
        dispatcher.forward(request, response);

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Bearing> bearings = null;
        try {
            bearings = bearingDao.getAllBearing();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("bearings", bearings);
        System.out.println(bearings);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/shopping.jsp");
        dispatcher.forward(request, response);
    }
}
